package com.wust.smartcity.generatorcode.task;

import com.wust.smartcity.generatorcode.task.base.AbstractTask;
import com.wust.smartcity.generatorcode.utils.ConfigUtil;
import com.wust.smartcity.generatorcode.utils.FileUtil;
import com.wust.smartcity.generatorcode.utils.FreemarkerConfigUtils;
import com.wust.smartcity.generatorcode.utils.StringUtil;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：wust
 * @date：2019-12-26
 */
public class ServiceTask extends AbstractTask {

    public ServiceTask(String childEntityPackageName,String className) {
        super(childEntityPackageName,className);
    }

    @Override
    public void run() throws IOException, TemplateException {
        String entityPackageName = ConfigUtil.getConfiguration().getPath().getEntityPackageName();
        if(childEntityPackageName != null && childEntityPackageName.replaceAll(" ","").length() > 0){
            String childEntityPackageNameTemp = childEntityPackageName;
            entityPackageName = entityPackageName + childEntityPackageNameTemp.replaceAll("_",".");
        }


        // 生成Service填充数据
        Map<String, String> serviceData = new HashMap<>();
        serviceData.put("BasePackageName", ConfigUtil.getConfiguration().getPackageName());
        serviceData.put("ServicePackageName", ConfigUtil.getConfiguration().getPath().getService());
        serviceData.put("EntityPackageName", entityPackageName);
        serviceData.put("DaoPackageName", ConfigUtil.getConfiguration().getPath().getDao());
        serviceData.put("Author", ConfigUtil.getConfiguration().getAuthor());
        serviceData.put("DateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        serviceData.put("ClassName", className);
        serviceData.put("EntityName", className);
        serviceData.put("EntityNameForCamelCase", StringUtil.firstToLowerCase(className));
        String filePath = FileUtil.getSourcePath() + StringUtil.package2Path(ConfigUtil.getConfiguration().getPackageName()) + StringUtil.package2Path(ConfigUtil.getConfiguration().getPath().getService());
        String fileName;
        if (StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getInterf())) { // 表示不生成Service接口文件
            serviceData.put("Impl", "");
            serviceData.put("Override", "");
            serviceData.put("InterfaceImport", "");
            fileName = className + "Service.java";
        } else {
            serviceData.put("Override", "\n    @Override");
            serviceData.put("InterfaceImport", "import " + ConfigUtil.getConfiguration().getPackageName() + ConfigUtil.getConfiguration().getPath().getInterf() + "." + className + "Service;");
            fileName = className + "ServiceImpl.java";
        }
        // 生成Service文件
        FileUtil.generateFile(FreemarkerConfigUtils.TYPE_SERVICE, serviceData, filePath + fileName);
    }
}
